/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	heyong 
 * 
 * <p>描   述  :   NetworkTransformServiceImpl.java for com.pengtu.gsj.service.impl.check.yh.roadnet    </p>
 * 
 * <p>最后修改 : $: Aug 25, 2017-9:42:08 AM v 1.0.0	 heyong   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.yh.roadnet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.roadnet.NetworkTransformService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * NetworkTransformServiceImpl
 * 养护管理-路网结构改在工程-路网改造制度实现层
 * Aug 25, 2017 9:42:08 AM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class NetworkTransformServiceImpl implements NetworkTransformService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	/**
	 * 判断路面结构制度表判分和导出
	 */
	@Override
	public Map<String, Object> getNetworkTransformMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		
		List<List<String>> attmentList = new ArrayList<List<String>>();
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取改造工程管理方法文件
		List<Attachment> glffList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "lwgz_glff");
		//获取年报文件
		List<Attachment> nbList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "lwgz_nb");
		
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 0)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		double total = yearPart; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull + yearPart + poitMark * years.length;	//满分
		
		if (glffList.size() > 0 ) {
			total += yearFull;
		}
		double nbMark = 0.0;
		for ( int i = 0; i<years.length; i++){
			List<String> checkList = Arrays.asList("0", "0", "0");
			checkList.set(0, String.valueOf(years[i]));
			int temp = 0;
			//判断每年是否有年报文件
			if (nbList.size() > 0) {
				for (int j = 0; j< nbList.size(); j++) {
					if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", nbList.get(j).getFileTime()))) {
						checkList.set(1, "1");
						temp ++ ;
						break;
					} 
				}
			}
			total +=  poitMark * temp;
			nbMark += poitMark * temp;
			checkList.set(2, String.valueOf(MathsUtils.formatDouble(nbMark, 2)));
			attmentList.add(checkList);
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(MathsUtils.formatDouble(total,2) *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("percent", percentage);
		map.put("yearFull", yearFull);
		map.put("yearPart", yearPart);
		map.put("attmentList", attmentList);
		map.put("glffList", glffList);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
